#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class A
{
public:
	//构造函数
	A(int a = 0)
		:_a(a)//使用成员初始化列表：_a(a),初始化成员变量_a
	//this是指向当前对象的指针
	{
		cout << "A():"<<this << endl;
	}
    #3
	//析构函数
	//当对象销毁时自动调用
	~A()
	{
		cout << "~A():" << this << endl;
	}
private:
	//定义
	int _a;
};


//int main()
//{
//	A* P1 = new A(10);//动态分配空间，并初始化为10
//	A* P2 = (A*)operator new (sizeof(A));//与上面的语句相同
//	new(P1)A(10);//调用构造函数A（int）并传入参数10
//	delete P1;//对象销毁，调用析构，释放内存
//	//P1->~A;与上面相同
//	operator delete(P1);//直接调用底层内存释放函数，不用调用析构函数，与operator new配对使用
//	return 0;
//}

#
//基本用法
//1.自动类型推导
//2.用于迭代器
//3.高级用法与引用结合
//int a = 10;
//auto& ref = a;ref是int& 类型
//const auto& cref = a;cref是const int&类型
//auto变量必须初始化
//void func2(auto a)
//{}
//auto func1()
//{
//	return 1;
//}
//auto func2()
//{
//	return func1;
//}
//auto func3()
//{
//	return func2;
//}
//int main()
//{
//	int x = 10;
//	auto y = &x;
//	//右边必须是指针
//	auto* z = &x;
//	auto& m = x;
//	auto n = x;
//	auto ret = func3();
//	return 0;
//}
//int main()
//{
//	int array[] = { 1,2,3,4,5 };
//	for(int i=0;i<sizeof(array)/sizeof(int);++i)
//	{
//		array[i] *= 2;
//	}
//	for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i)
//	{
//		cout << array[i] << "";
//	}
//	cout << endl;
//	//范围for
//	for (auto& e : array)
//	{
//		e *= 2;
//	}
//	cout << endl;
//	for (auto e : array)
//	{
//		cout << e << " ";
//	}
//	cout << endl;
//	for (const auto& e : array)
//	{
//		cout << e << " ";
//	}
//	cout << endl;
//	return 0;
//}
//int main()
//{
//	cout << sizeof(wchar_t) << endl;
//	cout << sizeof(char16_t) << endl;
//	cout << sizeof(char32_t) << endl;
//}
#include<string>
#include<list>
#include<assert.h>
namespace zzj
{
	class string
	{
	public:
		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}
	private:
		char* _str;
		size_t _size;
		size_t _capacity;
	};
}

void test_string1()
{
	string s1;
	string s2("hello world");
	string s3(s2);
	string s4(s2, 6);
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	//1.下标+[]
	for (size_t i = 0; i < s2.size(); i++)
	{
		s2[i] += 1;//ASCLL码
		//s2.operator[](i)+=1;运算符重载，重载[]符号
	}
	for (size_t i = 0; i < s2.size(); i++)
	{
		cout << s2[i] << " ";
	}
	cout << endl;
	const string s5("xxxxxxx");
	auto it = s2.begin();
	while (it != s2.end())
	{
		*it += 1;
		cout << *it << " ";
		++it;
	}
	cout << endl;


	string::reverse_iterator rit = s2.rbegin();//获取反向迭代器起始位置
	while (rit != s2.rend())//循环到结尾
	{
		cout << *rit << " ";//解引用迭代器，打印当前字符
		++rit;
	}
	cout << endl;//换行

	//链表正向遍历
	//表示list<int>的迭代器类型
	//list<int>lt1 = { 1,2,3,4,5 };
	//list<int>::iterator it1 = lt1.begin();
	//while (it1 != lt1.end())
	//{
		//cout << *it1 << " ";
		//++it1;
	//}
	//cout << endl;
	
	list<int>lt1 = { 1,2,3,4,5 };
	list<int>::iterator it1 = lt1.begin();
	while (it1 != lt1.end())
	{
		cout << *it1 << " ";
		++it1;
	}
	cout << endl;
	for (auto ch : s2)//auto 会自动推导为char类型
		//每次循环ch会依次绑定到s2的每个字符
	{
		cout << ch << " ";
	}
	cout << endl;
}
int main()
{
	test_string1();
	return 0;
}
void test_string2()
{
	string s1;
	cout << s1.max_size() << endl;

	string s2("hello world");
	cout << s2.size() << endl;
	cout << s2.capacity() << endl;//为什么要使用（）
	//因为size和capacity是成员函数
	//阻塞
	int i = 0;
	cin >> i;
	cout << s2.at(i) << endl;// 输出字符串 s2 的第 i 个字符
	cout << s2.size() << endl;
	cout << s2.capacity() << endl;
}

void test_string3()
{
	string s1;
	s1.push_back('x');
	s1.push_back('x');
	s1.push_back('x');
	s1.append("yyyyyy");
	cout << s1 << endl;
	string s2("hello world");
	s1.append(s2.begin() + 6, s2.end());
	cout << s1 << endl;

	s1 += ' ';
	s1 += "zzz";
	s1 += s2;
	cout << s1 << endl;                      
}

void test_string4()
{
	string s1("11111111111");
	string s2("2222222222222222222222222222222222222222");
	cout << s2.size() << endl;//40
	cout << s2.capacity() << endl;//47
	s2.reserve(80);//开空间
	cout << s2.size() << endl;//40
	cout << s2.capacity() << endl;//95

	string s;
	s.reserve(1000);
	size_t old = s.capacity();
	cout << "capacity changed:" << old << '\n';
	cout << "making s grow:\n";
	for (int i = 0; i < 1000; ++i)
	{
		s += 'c';
		if (old != s.capacity())
		{
			old = s.capacity();
			cout << "capacity changed:" << old << '\n';
		}
	}
}

void test_string5()
{
	string s2("2222222222");
	cout << s2.size() << endl;
	cout << s2.capacity() << endl;
	s2.resize(23, 'x');//调整大小并指定填充字符
	cout << s2.size() << endl;
	cout << s2.capacity() << endl;
}

void test_string6()
{
	string s1("2222222222");
	cout << s1 << endl;
	s1.insert(0, "hello");
	cout << s1 << endl;
	s1.insert(0, 2, 'x');//在0的位置插入两个x
	cout << s1 << endl;
	s1.erase(5, 2);
	cout << s1 << endl;
	s1.erase(5);
	cout << s1 << endl;
	s1.erase(0, 1);
	s1.erase(s1.begin());//头删
	cout << s1 << endl;
	cout << isalpha('a') << endl;//检查给定的字符是否是字母字符
	cout << isalpha('1') << endl;//是字母返回1，不是则返回0
}
void test_string7()
{
	//string s1("2222222222");
	//string s2("333333333");
	//s1 = s2;
	//s1.assign(s2);//与=功能相同
	//cout << s1 << endl;
	//cout << s2 << endl;
	
	//将空格替换为%%
	string s2("hello    world hello  gfx");
	//size_t pos = s2.find(' ');
	//while (pos != string::npos)//库里面的静态常量，表示未找到。也是size_t类新房
	//{
	//	s2.replace(pos, 1, "%%");//
	//	pos = s2.find(' ', pos + 2);//移动pos的位置
	//}
	//cout << s2 << endl;


	//string s3("hello    world hello  gfx");
	//size_t ipos = s3.find(' ');
	//while (ipos != string::npos)//库里面的静态常量，表示未找到。也是size_t类新房
	//{
	//	s3.replace(ipos, 3, "%%");
	//	//pos表示替换的起始位置
	//	//3表示替换的字符数量
	//	//%%表示替换后的字符串
	//	ipos = s3.find(' ', ipos + 2);//移动pos的位置
	//}
	//cout << s3 << endl;

	string s3;
	for (auto ch : s2)//依次取出 s2 中的每个字符，赋值给 ch（类型自动推导为 char）
	{
		if (ch == ' ')
			s3 += "%%";
		else
			s3 += ch;
	}
	cout << s3 << endl;

	s2 = s3;
	s2.swap(s3);
	cout << s2 << endl;
}
int main()
{
	test_string4();
	return 0;
}